% fname: routing_dyad_0_6__sel_bufferlevel__topology_8x8__.m
% ./noxim -routing dyad 0.6 -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 4 -hs 6 0.2 -hs 7 0.2 -hs 14 0.2 -hs 15 0.2 

function [max_pir, max_throughput, min_delay] = routing_dyad_0_6__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0015        19.6069      0.0134353            126            753           6019
           0.0015        20.0052      0.0120952             92            763           6096
           0.0015        21.1638      0.0132716            109            769           6158
           0.0015        20.9974      0.0138705            113            777           6214
           0.0015        20.8269      0.0136754            134            780           6236
           0.0015        19.7741      0.0126716            116            748           5981
           0.0015         20.907      0.0132604            101            796           6365
           0.0015        20.7788      0.0124958            103            737           5898
           0.0015        21.2903       0.012502            109            775           6201
           0.0015        20.3422      0.0125313             89            751           6015
           0.0015        21.2718      0.0124113            107            769           6156
           0.0015        21.2234       0.013431             98            779           6232
           0.0015        21.5752       0.012625            117            758           6060
           0.0015         20.184      0.0119008            106            750           5998
           0.0015        21.5263      0.0126708            112            760           6082
           0.0015        20.0159        0.01365             89            819           6552
           0.0015        20.3884      0.0135746             83            775           6190
           0.0015        21.7671      0.0125423             95            777           6221
           0.0015        20.0474      0.0127177             89            738           5901
           0.0015        19.7053      0.0127771             76            767           6133
           0.0017        23.4802      0.0142339            126            883           7060
           0.0017        24.0066      0.0153708            159            909           7255
           0.0017        22.0954      0.0142582            107            870           6958
           0.0017         22.388      0.0149332            151            866           6929
           0.0017        22.3127      0.0143258            144            873           6991
           0.0017          22.53      0.0136414            107            832           6657
           0.0017        22.1981      0.0141229            167            833           6666
           0.0017        21.4233      0.0140388            104            815           6514
           0.0017        24.2639      0.0141598            179            864           6910
           0.0017        21.6929      0.0145922            129            889           7121
           0.0017        25.9765      0.0158729            205            937           7492
           0.0017        23.4615      0.0142979            159            858           6863
           0.0017        21.7831      0.0143689             95            876           7012
           0.0017        21.6204      0.0144083            120            864           6916
           0.0017        21.4129      0.0134262            158            821           6552
           0.0017        21.4345      0.0136492            112            847           6770
           0.0017        23.3028      0.0143667            161            862           6896
           0.0017        21.0652      0.0140792            132            844           6758
           0.0017        21.1143       0.014379            127            892           7132
           0.0017        21.1094      0.0130328            101            795           6360
           0.0019        25.2447      0.0162766            157            993           7943
           0.0019        26.1534      0.0156025            184            952           7614
           0.0019        25.6963       0.016121            130           1001           7996
           0.0019        25.6034      0.0152117            468            943           7545
           0.0019        22.4795      0.0167889            122           1024           8193
           0.0019        31.1148      0.0162854            537            976           7817
           0.0019         24.052        0.01575            120            961           7686
           0.0019        24.0251      0.0156475            124            955           7636
           0.0019        25.4731      0.0155625            144            966           7719
           0.0019        22.6823      0.0149551            159            957           7657
           0.0019        28.4509       0.015125            470            967           7744
           0.0019        25.3487      0.0160685            298            995           7970
           0.0019         23.097      0.0151371            125            938           7508
           0.0019        26.6146      0.0160703            164           1030           8228
           0.0019        24.0526      0.0155717            152            951           7599
           0.0019        24.8594      0.0151131            163            953           7617
           0.0019        24.2274      0.0151774            175            941           7528
           0.0019        26.4908       0.016625            291            980           7847
           0.0019        23.7159      0.0163176            103            996           7963
           0.0019        24.6914      0.0158851            119            985           7879
           0.0021        27.6422      0.0166389            178           1048           8386
           0.0021        28.0866      0.0170556            208           1074           8596
           0.0021        25.4009      0.0170996            180           1095           8755
           0.0021        26.6419      0.0172718            163           1089           8705
           0.0021        25.5558      0.0162143            173           1022           8172
           0.0021        27.5725      0.0168532            240           1062           8494
           0.0021        27.3251      0.0164355            199           1052           8415
           0.0021        29.4757      0.0173848            189           1112           8901
           0.0021        27.4029      0.0171621            174           1097           8787
           0.0021        26.0405      0.0163427            189           1012           8106
           0.0021        26.4459      0.0172766            192           1054           8431
           0.0021        27.2097      0.0173254            192           1092           8732
           0.0021        25.8094      0.0170389            190           1039           8315
           0.0021        29.8732      0.0167738            178           1057           8454
           0.0021        31.2185      0.0180258            269           1135           9085
           0.0021         28.971      0.0175258            236           1104           8833
           0.0021        25.9647      0.0172695            162           1105           8842
           0.0021        25.8923      0.0180958            144           1086           8686
           0.0021          30.04      0.0188156            149           1149           9182
           0.0021        27.8138      0.0163651            220           1031           8248
           0.0023        32.0161      0.0187599            371           1181           9455
           0.0023        35.5844      0.0189901            575           1196           9571
           0.0023         33.963       0.019621            332           1217           9732
           0.0023        37.8619      0.0193281            338           1238           9896
           0.0023         36.789      0.0195675            403           1232           9862
           0.0023        37.8408      0.0195278            320           1231           9842
           0.0023        35.0136      0.0193115            268           1177           9424
           0.0023        32.3453      0.0182734            281           1170           9356
           0.0023        36.2278      0.0189266            414           1194           9539
           0.0023        34.5547      0.0183056            510           1152           9226
           0.0023        30.4525       0.018459            257           1180           9451
           0.0023        30.1755      0.0174484            211           1100           8794
           0.0023         30.625      0.0182598            214           1168           9349
           0.0023        35.7971      0.0187421            546           1183           9446
           0.0023        30.6739      0.0186934            186           1196           9571
           0.0023        37.6446      0.0187083            595           1179           9429
           0.0023        34.0284       0.017873            393           1126           9008
           0.0023        38.2076      0.0197646            480           1185           9487
           0.0023        29.9452       0.019175            280           1150           9204
           0.0023        30.5128      0.0179425            447           1129           9043
           0.0025        33.9728      0.0206602            241           1322          10578
           0.0025        35.0912      0.0200238            288           1261          10092
           0.0025        37.5757      0.0205927            464           1275          10214
           0.0025        34.2075      0.0193571            326           1219           9756
           0.0025        42.3153       0.019998            675           1278          10239
           0.0025        34.8246      0.0205278            388           1294          10346
           0.0025        49.4065      0.0209802            709           1321          10574
           0.0025        36.1209      0.0200141            345           1241           9927
           0.0025        42.0179       0.020379            473           1285          10271
           0.0025        43.3374      0.0208433            491           1313          10505
           0.0025        39.6769      0.0204277            561           1309          10459
           0.0025        45.1286      0.0212242            656           1338          10697
           0.0025        46.1802      0.0204688            759           1310          10480
           0.0025        55.5335      0.0210254            805           1344          10765
           0.0025        47.8218      0.0208413            958           1313          10504
           0.0025        42.4535      0.0213651            357           1345          10768
           0.0025        35.4221           0.02            270           1258          10080
           0.0025        43.9432      0.0217324            491           1391          11127
           0.0025        43.1399      0.0201875            439           1251          10013
           0.0025        37.8651       0.020498            406           1312          10495
           0.0027        40.9643      0.0213651            486           1346          10768
           0.0027        59.7948      0.0232336            756           1418          11338
           0.0027        39.7103      0.0209841            413           1322          10576
           0.0027        64.4745      0.0221089           1549           1372          10966
           0.0027        42.3969      0.0216429            500           1363          10908
           0.0027        49.7542      0.0223145            586           1383          11068
           0.0027        56.9417      0.0214707            783           1372          10993
           0.0027        41.4617      0.0212012            378           1358          10855
           0.0027        50.8686      0.0218906            616           1400          11208
           0.0027        40.2151      0.0214008            475           1348          10786
           0.0027        47.7894      0.0210215            620           1344          10763
           0.0027        41.9159      0.0217422            359           1392          11132
           0.0027        56.6248      0.0221523            571           1418          11342
           0.0027        44.9081      0.0221133            755           1415          11322
           0.0027        44.3034      0.0225992            412           1424          11390
           0.0027        42.7601      0.0212969            636           1363          10904
           0.0027          45.18       0.021869            634           1378          11022
           0.0027        62.6494      0.0227097            730           1406          11264
           0.0027        45.2579      0.0210813            449           1330          10625
           0.0027        52.4602      0.0213887            602           1369          10951
           0.0029        92.0744      0.0239395           1356           1532          12257
           0.0029        62.5283      0.0229121           1074           1467          11731
           0.0029        60.2533      0.0240298            727           1512          12111
           0.0029         78.848      0.0230176            973           1474          11785
           0.0029        49.5755       0.022209            673           1423          11371
           0.0029        63.6819      0.0236653            902           1468          11738
           0.0029        69.8901      0.0227285            899           1456          11637
           0.0029        76.6346       0.023834           1956           1527          12203
           0.0029        65.6582      0.0233145           1055           1492          11937
           0.0029        74.3844      0.0232344            699           1488          11896
           0.0029        62.7119      0.0242202            930           1527          12207
           0.0029        112.441      0.0242324           1431           1549          12407
           0.0029        60.0298      0.0241406            539           1543          12360
           0.0029        56.6064      0.0224297            832           1438          11484
           0.0029        104.001      0.0233992           1483           1451          11606
           0.0029        71.6065      0.0233887           1519           1497          11975
           0.0029        75.6763      0.0237383           1208           1520          12154
           0.0029        57.3522      0.0226699            561           1451          11607
           0.0029        70.6504      0.0224375            963           1436          11488
           0.0029        97.9942      0.0244921           1804           1542          12344
           0.0031        115.942      0.0254219           1574           1629          13016
           0.0031        105.986      0.0247207           2651           1581          12657
           0.0031        92.9167      0.0247559           1642           1584          12675
           0.0031        95.8136      0.0246746           1195           1556          12436
           0.0031        69.6139      0.0240938            889           1541          12336
           0.0031         105.78      0.0247996           1557           1562          12499
           0.0031        82.6716      0.0232031            957           1486          11880
           0.0031        121.962      0.0253086           1843           1620          12958
           0.0031        75.3149      0.0251875            928           1610          12896
           0.0031        97.7088      0.0243535           2015           1559          12469
           0.0031        82.0803      0.0241349           2129           1520          12164
           0.0031        188.231      0.0251602           3477           1609          12882
           0.0031        163.175      0.0257676           3647           1650          13193
           0.0031        118.879      0.0247109           1437           1581          12652
           0.0031        179.448      0.0252679           3453           1591          12735
           0.0031        199.395      0.0250117           3046           1601          12806
           0.0031         106.36      0.0252937           1685           1591          12748
           0.0031        97.5575      0.0261367           1218           1670          13382
           0.0031        136.837       0.025459           1887           1628          13035
           0.0031        89.4256      0.0254277           2643           1626          13019
           0.0033        144.834      0.0254297           2869           1630          13020
           0.0033        184.154      0.0262578           2224           1680          13444
           0.0033        200.244      0.0258887           3872           1656          13255
           0.0033        135.895      0.0262988           2483           1683          13465
           0.0033        100.578      0.0261914           1309           1676          13410
           0.0033        142.045      0.0257539           2180           1649          13186
           0.0033        263.564      0.0273223           3396           1749          13989
           0.0033        105.438      0.0253652           2374           1623          12987
           0.0033        203.787      0.0248574           3924           1590          12727
           0.0033        162.876      0.0257773           2065           1652          13198
           0.0033        231.845       0.026881           3602           1694          13548
           0.0033          276.8      0.0256191           4660           1640          13117
           0.0033        226.915       0.026752           3658           1713          13697
           0.0033        156.265      0.0265039           2279           1697          13570
           0.0033        193.706      0.0255332           5328           1633          13073
           0.0033        187.545      0.0257852           3012           1651          13202
           0.0033        312.567      0.0270605           4520           1730          13855
           0.0033        117.406      0.0260417           1849           1642          13125
           0.0033        142.358      0.0250547           3364           1604          12828
           0.0033        278.304      0.0267579           3166           1685          13486
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
